﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Text;

namespace API.Controllers
{
    public class PatientDiagnosisController : ApiController
    {
        /// <summary>
        /// Intializing Helper Classes - DB Connection and Security Provider for Encrypting and Decrypting Passwords
        /// Pass in DBServerName, DBUsern
        /// ame and DBPassword from Config File for DB connections
        /// The Security helper does nto take any parameters
        /// </summary>
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());

        [ActionName("PatientDiagnosis")]

        public AddReferralDiagonsisResponse PostPatientDiagnosis([FromBody]AddReferralDiagonsisRequest request)
        {
            AddReferralDiagonsisResponse response = new AddReferralDiagonsisResponse();
            try {
                ArrayList FinalList = new ArrayList();

                StringBuilder sb = new StringBuilder();

                string[] SelectedDiagnosis = request.DiagnosisIDs.Split(',');

                if (request.DiagnosisIDs.Length > 1)
                {
                    foreach (string diag in SelectedDiagnosis)
                    {
                        string a = diag.Substring(diag.IndexOf("("), diag.Length - diag.IndexOf("("));
                        a = a.Replace("(", "");
                        a = a.Replace(")", "");
                        sb.Append(a.Trim() + ",");
                    }
                }

                dsComm.AddParameter("@ParamReferralID", request.ReferralID);
                dsComm.AddParameter("@ParamDiagnosisID", sb.ToString());
                dsComm.AddParameter("@ParamDiagnosisNotes", request.DiagnosisNotes);
                int i = dsComm.ExecuteNonQuery("dbo.spPostPatientDiagnosis");

                if (i > 0)
                {
                    response.ErrorMessage = "Saved";
                }
                else
                {
                    response.ErrorMessage = "NotSvaed";
                }
            }
            catch(Exception ex)
            {
                response.ErrorMessage = ex.StackTrace + " Message-" + ex.Message;
            }
            return response;
        }


        public AddReferralDiagonsisResponse GetPatientDiagnosis(int id)
        {
            AddReferralDiagonsisResponse response = new AddReferralDiagonsisResponse();

            dsComm.AddParameter("@ParamReferralID", id);

            string data = dsComm.ExecuteStoredProcedure("dbo.spGetFilteredDiagnosis");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet DiagnosisDataSet = new DataSet();
            DiagnosisDataSet.ReadXml(xReader);

            if(DiagnosisDataSet.Tables.Count > 0)
            {
                response.PatientDiagnosis = DiagnosisDataSet.Tables[0];
                response.ErrorMessage = "Found Data";
                if(DiagnosisDataSet.Tables.Count > 1)
                {
                    ArrayList ls = new ArrayList();
                    DataTable tb = DiagnosisDataSet.Tables[1];
                    for(int i = 0; i< tb.Rows.Count;i++)
                    {
                        ls.Add(tb.Rows[i][0].ToString());
                    }

                    response.ChipData = ls.ToArray();
                }
            }
            else
            {
                response.PatientDiagnosis = new DataTable();
                response.ErrorMessage = "No Data";
            }
            return response;
        }
        
        /// <summary>
        /// Options response for the preflight request of a CORS Opertaions. Will be standard across various functions
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage OptionsPatientDiagnosis()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }
    }
}
